package main

/*
将监听的tcp协议改为http协议
*/

import (
	"io"
	"net/http"
	"net/rpc"
	"net/rpc/jsonrpc"
)

// HelloService 定义一个简单结构体
type HelloService struct{}

// Hello 给结构体封装方法
func (s *HelloService) Hello(request string, reply *string) error {
	//修改入参指向的值
	*reply = "Hello " + request
	return nil
}

func main() {
	//将结构体HelloService{}对象注册到rpc中，名称(随便)：HelloService_1 ( 这里是2、注册处理逻辑handle )
	_ = rpc.RegisterName("HelloService_1", &HelloService{})
	http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
		var conn io.ReadWriteCloser = struct {
			io.Writer
			io.ReadCloser
		}{
			ReadCloser: r.Body,
			Writer:     w,
		}
		//处理请求
		rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
	})
	//启动服务
	http.ListenAndServe(":1234", nil)

}
